<?php
namespace zzcms\primary\controller;
/**
 * 文章新闻类
 * Class News
 * @package zzcms\home\controller
 */
class News extends Base
{
    protected $model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new \zzcms\admin\model\News();
    }


    /**
     * 关键词搜索
     * url:http://www.posji.com/apimobile/news/lists.html
     *     http://www.sosoao.com/apimobile/news/lists.html
     * 必传参数: keyword：关键词
     * 选传参数:无
     * 返回值
     *   code：返回状态
     *   msg：提示信息
     *   data：返回信息    keyword：关键词  hot_keywords：热门搜索   keywords：下拉关键词  lists：搜索结果列表
     *
     */
    public function lists()
    {
        $where = [];
        $page = isset($this->info['page']) ? max(intval($this->info['page']), 1) : 1;
        $pagesize = isset($this->info['pagesize']) ? intval($this->info['pagesize']) : 10;
        $cateid = isset($this->info['cateid']) ? intval($this->info['cateid']) : 0;
        if ($cateid > 0) {
            $where[] = ['cate_id', '=', $cateid];
        }
        $cate_cache = getCache('category');
        $lists = $this->model->where($where)->page($page, $pagesize)->order('id desc')->select();
        $count = $this->model->where($where)->count();
        foreach ($lists as $k => $item) {
            $item['catename'] = isset($cate_cache[$item['cate_id']]) ? $cate_cache[$item['cate_id']]['catename'] : '';
            $item['image'] = empty($item['thumb']) ? '/static/images/empty.png' : $item['thumb'];
            $item['date'] = date('Y-m-d', strtotime($item['update_time']));
            $lists[$k] = $item;
        }
        $this->array2json(['code' => 0, 'data' => $lists, 'count' => $count]);
    }

    /**
     * 文档内容
     * url:http://www.xmyeditor.com/apimobile/help/detail.html
     * 必传参数: id：文档ID
     * 选传参数:无
     * 返回值
     *   code：返回状态
     *   msg：提示信息
     *   data：返回信息    info：信息内容
     *
     */
    public function detail()
    {
        $id = isset($this->info['id']) ? intval($this->info['id']) : 0;
        if ($id < 1) {
            $this->array2json(['code' => 1, 'msg' => '缺少参数']);
        }
        $info = $this->model->getDetail($id);
        if (empty($info)) {
            $this->array2json(['code' => 1, 'msg' => '信息不存在']);
        }
        //计数+1
        $this->model->where(['id' => $id])->setInc('hits');
        $info['content'] = htmlspecialchars_decode($info['content']);
        $info['thumb'] =  '/static/images/bannar1.png';
        //分类名称
        $cate_cache = getCache('category');
        $info['catename'] = isset($cate_cache[$info['cate_id']]) ? $cate_cache[$info['cate_id']]['catename'] : '';
        $info['date'] = date('Y-m-d', strtotime($info['update_time']));
        $this->array2json(['code' => 0, 'msg' => 'ok', 'data' => $info]);
    }
}
